package com.yuexunit.finance.member.service;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.yuexunit.finance.member.entity.EnterpriseAddressEntity;
import com.yuexunit.inject.annotation.Implements;
import com.yuexunit.page.PageCriteria;
import com.yuexunit.page.PageList;
import com.yuexunit.persist.PersistUtil;
import com.yuexunit.persist.annotation.Transactional;
import com.yuexunit.util.DateUtil;

@Implements(EnterpriseAddressService.class)
public class EnterpriseAddressServiceImpl implements EnterpriseAddressService {

	@Override
	public List<EnterpriseAddressEntity> inquireEnterpriseAddressList(Long enterpriseID) {
		DetachedCriteria criteria = DetachedCriteria.forClass(EnterpriseAddressEntity.class);
		if (enterpriseID != null) {
			criteria.add(Restrictions.eq("enterpriseID", enterpriseID));
		}
		return PersistUtil.get(criteria);
	}

	@Override
	@Transactional
	public void createAddressInfo(EnterpriseAddressEntity entity) {
		entity.setCreateDateTime(DateUtil.now());
		entity.setUpdateDateTime(DateUtil.now());
		PersistUtil.insert(entity);
	}

	@Override
	@Transactional
	public void updateAddressInfo(EnterpriseAddressEntity entity) {
		entity.setUpdateDateTime(DateUtil.now());
		PersistUtil.update(entity);
	}

	@Override
	public EnterpriseAddressEntity inquireAddressInfoDetail(Long addressID) {
		DetachedCriteria criteria = DetachedCriteria.forClass(EnterpriseAddressEntity.class);
		criteria.add(Restrictions.eq("addressID", addressID));
		List<EnterpriseAddressEntity> list = PersistUtil.get(criteria);
		if (list != null && list.size() > 0) {
			return list.get(0);
		} else {
			return null;
		}
		
	}

	@Override
	@Transactional
	public void deleteAddressInfo(Long addressID) {
		PersistUtil.delete(EnterpriseAddressEntity.class, addressID);
	}

	@Override
	public PageList<EnterpriseAddressEntity> inquireEnterpriseAddressPageList(
			Long enterpriseID,PageCriteria page) {
		DetachedCriteria criteria = DetachedCriteria.forClass(EnterpriseAddressEntity.class);
		if(enterpriseID != null) {
			criteria.add(Restrictions.eq("enterpriseID", enterpriseID));
		}
		PageList<EnterpriseAddressEntity> pageList = PersistUtil.get(criteria, page);
		return pageList;
	}

}
